Statistical models that explicitly account for errors in the measurement of predictor variables.
General Principles
Measurement error refers to the variability in the measurement of a variable, and measurement error can be generated by several factors, such as sampling bias, censoring bias, and group size heterogeneity. It is an important consideration in many fields, including statistics, economics, and engineering, where accurate measurements are crucial for making informed decisions. To account for measurement error, we can use a measurement error model. This model assumes that the measurement of a variable is subject to an error, which can be modeled using a probability distribution. The model can be used to estimate the parameters of the measurement error distribution, such as the mean and variance, and to make predictions about the measurements based on the estimated parameters. Measurement error models are composed models (i.e., models with sub-models) that evaluate different generative processes, starting with the measurement error process, which is then used to generate the observed data.
Example
Below is an example code snippet demonstrating a Bayesian measurement error model using the BayesForge (BF) package. The data consist of three continuous variables (marriage rate, divorce rate, age), and the goal is to estimate the effect of age and marriage rate on the divorce rate while considering that the divorce rate has a measurement error. This example is based on McElreath (2018).
from BayesForge import bfimport jax.numpy as jnp# Setup device------------------------------------------------m = bf(platform='cpu')# Import Data & Data Manipulation ------------------------------------------------# Importdata_path = m.load.WaffleDivorce(only_path=True)m.data(data_path, sep=';') m.scale(['MedianAgeMarriage', 'Marriage']) # Scaledat =dict( D_obs = m.z_score(m.df['Divorce'].values), D_sd = jnp.array(m.df['Divorce_SE'].values / m.df['Divorce'].std()), A = jnp.array(m.df['MedianAgeMarriage'].values), M = jnp.array(m.df['Marriage'].values), N = m.df.shape[0] )m.data_on_model = dat # Send to model (convert to jax array)# Define model ------------------------------------------------def model(D_obs, D_sd, A, N, M): a = m.dist.normal(0, 0.2, name ='a') beta = m.dist.normal(0, 0.5, name ='beta') eta = m.dist.normal(0, 0.5, name ='eta') s = m.dist.exponential(1, name ='s') mu = a + beta * A + eta * M D_true = m.dist.normal(mu, s, name ='D_true') m.dist.normal(D_true , D_sd, obs = D_obs) # Run MCMC ------------------------------------------------m.fit(model, progress_bar=False) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
bf v 0.0.60 package loaded
jax.local_device_count(cpu) = 32
data(): renamed columns so they can be model arguments: 'Marriage SE' -> 'Marriage_SE', 'Divorce SE' -> 'Divorce_SE'. The original names are still accepted by the methods of m.
mean
sd
eti89_lb
eti89_ub
ess_bulk
ess_tail
r_hat
mcse_mean
mcse_sd
D_true[0]
1.17
0.36
0.61
1.76
5977.20
2935.87
1.00
0.00
0.01
D_true[1]
0.68
0.55
-0.19
1.55
7015.62
3319.81
1.00
0.01
0.01
D_true[2]
0.43
0.33
-0.10
0.96
8598.54
3118.58
1.00
0.00
0.01
D_true[3]
1.42
0.46
0.69
2.17
7469.03
2732.97
1.00
0.01
0.01
D_true[4]
-0.90
0.13
-1.11
-0.70
10015.97
3001.42
1.00
0.00
0.00
D_true[5]
0.66
0.40
0.03
1.29
6590.12
3213.90
1.00
0.00
0.01
D_true[6]
-1.37
0.34
-1.91
-0.84
7591.83
3161.05
1.00
0.00
0.01
D_true[7]
-0.33
0.48
-1.12
0.45
9028.77
3461.97
1.00
0.01
0.01
D_true[8]
-1.87
0.58
-2.79
-0.92
5350.31
3331.94
1.00
0.01
0.01
D_true[9]
-0.62
0.16
-0.88
-0.36
9242.51
3150.08
1.00
0.00
0.00
D_true[10]
0.77
0.29
0.30
1.24
7193.60
2668.53
1.00
0.00
0.01
D_true[11]
-0.55
0.49
-1.35
0.22
5886.33
3208.48
1.00
0.01
0.01
D_true[12]
0.17
0.51
-0.65
0.96
3955.12
3335.93
1.00
0.01
0.01
D_true[13]
-0.87
0.22
-1.21
-0.52
7753.24
2902.84
1.00
0.00
0.00
D_true[14]
0.56
0.30
0.07
1.05
9248.15
3023.10
1.00
0.00
0.01
D_true[15]
0.29
0.38
-0.33
0.89
9402.05
3038.88
1.00
0.00
0.01
D_true[16]
0.50
0.42
-0.17
1.16
9141.37
3021.98
1.00
0.00
0.01
D_true[17]
1.26
0.34
0.71
1.81
7042.37
3243.09
1.00
0.00
0.01
D_true[18]
0.43
0.38
-0.17
1.03
7336.63
2636.57
1.00
0.00
0.01
D_true[19]
0.41
0.55
-0.44
1.32
4773.00
3343.50
1.00
0.01
0.01
D_true[20]
-0.55
0.32
-1.07
-0.03
8833.71
2806.00
1.01
0.00
0.01
D_true[21]
-1.09
0.26
-1.51
-0.68
8046.12
3317.05
1.00
0.00
0.00
D_true[22]
-0.27
0.27
-0.69
0.16
8873.33
2955.54
1.00
0.00
0.01
D_true[23]
-1.01
0.29
-1.48
-0.54
7274.26
3133.54
1.00
0.00
0.00
D_true[24]
0.43
0.41
-0.21
1.11
8243.77
2389.92
1.00
0.00
0.01
D_true[25]
-0.03
0.32
-0.53
0.47
9258.61
3097.60
1.00
0.00
0.01
D_true[26]
-0.03
0.51
-0.83
0.78
8916.19
2934.22
1.00
0.01
0.01
D_true[27]
-0.15
0.39
-0.78
0.46
7286.04
3001.03
1.00
0.00
0.01
D_true[28]
-0.25
0.50
-1.01
0.57
8158.80
2960.24
1.00
0.01
0.01
D_true[29]
-1.80
0.24
-2.18
-1.43
8479.76
3052.28
1.00
0.00
0.00
D_true[30]
0.17
0.42
-0.50
0.85
9236.16
2740.57
1.00
0.00
0.01
D_true[31]
-1.66
0.17
-1.92
-1.40
8934.65
2676.65
1.00
0.00
0.00
D_true[32]
0.12
0.24
-0.27
0.51
10684.76
2507.87
1.00
0.00
0.00
D_true[33]
-0.07
0.51
-0.90
0.74
6418.84
3389.49
1.00
0.01
0.01
D_true[34]
-0.12
0.22
-0.47
0.23
7765.49
2685.86
1.00
0.00
0.00
D_true[35]
1.28
0.41
0.64
1.94
7190.01
2914.74
1.00
0.00
0.01
D_true[36]
0.23
0.35
-0.32
0.79
8766.85
2946.34
1.00
0.00
0.01
D_true[37]
-1.03
0.22
-1.38
-0.67
10257.13
3017.12
1.00
0.00
0.00
D_true[38]
-0.90
0.53
-1.73
-0.03
7238.06
3344.19
1.00
0.01
0.01
D_true[39]
-0.68
0.32
-1.20
-0.19
9236.39
2990.46
1.00
0.00
0.01
D_true[40]
0.25
0.56
-0.64
1.13
6946.63
3330.48
1.00
0.01
0.01
D_true[41]
0.74
0.35
0.20
1.30
8133.32
3020.32
1.00
0.00
0.01
D_true[42]
0.19
0.18
-0.10
0.48
10274.49
3358.88
1.00
0.00
0.00
D_true[43]
0.80
0.44
0.08
1.48
5261.49
3247.94
1.00
0.01
0.01
D_true[44]
-0.42
0.54
-1.24
0.44
8840.61
3076.00
1.00
0.01
0.01
D_true[45]
-0.39
0.26
-0.80
0.03
10225.37
2723.05
1.00
0.00
0.00
D_true[46]
0.13
0.31
-0.36
0.64
9752.00
2680.57
1.00
0.00
0.01
D_true[47]
0.56
0.47
-0.18
1.32
9244.26
2789.86
1.00
0.00
0.01
D_true[48]
-0.64
0.27
-1.06
-0.20
8725.11
3162.66
1.00
0.00
0.00
D_true[49]
0.85
0.59
-0.11
1.79
5698.91
3377.58
1.00
0.01
0.01
a
-0.05
0.10
-0.21
0.11
5248.35
3512.41
1.00
0.00
0.00
beta
-0.61
0.16
-0.86
-0.35
3606.35
3526.75
1.00
0.00
0.00
eta
0.06
0.17
-0.21
0.33
3723.44
3256.25
1.00
0.00
0.00
s
0.59
0.11
0.43
0.77
1793.36
1908.81
1.00
0.00
0.00
library(BayesForge)jnp = reticulate::import('jax.numpy')# Setup platform------------------------------------------------m=importBF(platform='cpu')# Import data ------------------------------------------------m$data(m$load$WaffleDivorce(only_path =TRUE), sep=';')m$scale(list('MedianAgeMarriage', 'Marriage'))m$data_on_model$D_obs = m$z_score(jnp$array(m$df['Divorce']))m$data_on_model$D_sd = jnp$array(m$df['Divorce_SE']) /sd(unlist(m$df['Divorce']))m$data_on_model$A = jnp$array(m$df['MedianAgeMarriage'])m$data_on_model$M = jnp$array(m$df['Marriage'])m$data_on_model$N =as.integer(nrow(m$df))# Define model ------------------------------------------------model <-function(D_obs, D_sd, A, N, M){ a = m$dist$normal(0, 0.2, name ='a') beta = m$dist$normal(0, 0.5, name ='beta') eta = m$dist$normal(0, 0.5, name ='eta') s = m$dist$exponential(1, name ='s') mu = a + beta * A + eta * M D_true = m$dist$normal(mu, s, name ='D_true') m$dist$normal(D_true , D_sd, obs = D_obs, name ='D_obs') }# Run MCMC ------------------------------------------------m$fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m$summary() # Get posterior distribution
usingPyBayesForge# Setup device------------------------------------------------m =importBF(platform="cpu")# Import Data & Data Manipulation ------------------------------------------------# Importdata_path = m.load.WaffleDivorce(only_path=true)m.data(data_path, sep=";") m.scale(["MedianAgeMarriage", "Marriage"]) # Scaledat =pydict( D_obs = m.z_score(m.df["Divorce"].values), D_sd = jnp.array(m.df["Divorce_SE"].values / m.df["Divorce"].std()), A = jnp.array(m.df["MedianAgeMarriage"].values), M = jnp.array(m.df["Marriage"].values), N = m.df.shape[0] )m.data_on_model = dat # Send to model (convert to jax array)# Define model ------------------------------------------------@BFfunctionmodel(D_obs, D_sd, A, N, M) a = m.dist.normal(0, 0.2, name ="a") beta = m.dist.normal(0, 0.5, name ="beta") eta = m.dist.normal(0, 0.5, name ="eta") s = m.dist.exponential(1, name ="s") mu = a + beta * A + eta * M D_true = m.dist.normal(mu, s, name ="D_true") m.dist.normal(D_true , D_sd, obs = D_obs) end# Run mcmc ------------------------------------------------m.fit(model) # Optimize model parameters through MCMC sampling# Summary ------------------------------------------------m.summary() # Get posterior distributions
Mathematical Details
Bayesian formulation
D_i^* \sim \text{Normal}(D_i, \varsigma_i)
D_i \sim \text{Normal}(\mu_i, \sigma)
\mu_i = \alpha + \beta A_i + \eta M_i
\sigma \sim \text{Normal}(1)
where:
D_i^* is the observed divorce rate.
D_i is the true divorce rate.
\mu_i is the mean of the true divorce rate.
\sigma is the standard deviation of the true divorce rate.
\alpha is the intercept term.
\beta is the regression coefficient for age.
\eta is the regression coefficient for marriage rate.
Model validation tests
Comparison with Stan: 15.Measuring error.py fits the same model with BayesForge and Stan and compares the posterior distributions (KL divergence) and parameter recovery.
Data generative model and inference model pipeline: 15.Measuring error.py simulates data from the model with known parameters (data generative model), fits the inference model to each simulated dataset and checks that the parameters are recovered.
Plot: comparison with Stan (15.Measuring error.py)
Plot: data generative model and inference model pipeline (15.Measuring error.py)
Notes
Note
This is an approach that can be extended to any kind of model previously described. For example, one could generate a Bernoulli measurement error model by generating a process for the probabilities of success and failure. We can even go further by potentially having an error rate that is present only in one of the two outcomes.
Reference(s)
McElreath, Richard. 2018. Statistical Rethinking: A Bayesian course with examples in R and Stan. Chapman; Hall/CRC.